package com.woniu.cms.auth.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.auth.core.entity.Company;
import com.woniu.cms.auth.core.entity.Position;
import com.woniu.cms.auth.core.entity.RbacUser;
import com.woniu.cms.auth.core.mapper.CompanyMapper;
import com.woniu.cms.auth.core.mapper.PositionMapper;
import com.woniu.cms.auth.core.mapper.RbacUserMapper;
import com.woniu.cms.auth.core.service.ICompanyService;
import com.woniu.cms.auth.core.service.IPositionService;
import com.woniu.cms.auth.core.service.IRbacUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cms.auth.core.vo.PageUser;
import com.woniu.cms.auth.core.vo.PageUserVo;
import com.woniu.cms.auth.core.vo.PermVo;
import com.woniu.cms.auth.core.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lilili
 * @since 2022-09-16
 */
@Service
public class RbacUserServiceImpl extends ServiceImpl<RbacUserMapper, RbacUser> implements IRbacUserService {
    @Resource
    private RbacUserMapper userMapper;
    @Resource
    private IPositionService positionService;
    @Resource
    private ICompanyService companyService;

    /**
     * 获取用户Vo
     *
     * @param id 用户id
     * @return {@link UserVo}
     */
    @Transactional
    @Override
    public UserVo getUserVo(Integer id) {
        UserVo userVo = userMapper.getUserVo(id);
        return userVo;
    }

    @Transactional
    @Override
    public PageUserVo getPageUserVo(Integer pageNo, Integer pageSize) {
        Page<PageUser> page = new Page(pageNo, pageSize);
        PageUser pageUser = new PageUser();
        Page<PageUser> ipage = userMapper.selectUserVoPage(page, pageUser);
        List<PageUser> list = ipage.getRecords();
        PageUserVo pageUserVo = new PageUserVo().setUsers(list)
                .setCurrent(ipage.getCurrent())
                .setPages(ipage.getPages())
                .setSize(ipage.getSize())
                .setTotal(ipage.getTotal())
                .setHasPrevious(ipage.hasPrevious())
                .setHasNext(ipage.hasNext());


        return pageUserVo;
    }

    @Transactional
    @Override
    public PageUserVo getLikePageUserVo(Integer pageNo, Integer pageSize, String like) {
        Page<PageUser> page = new Page(pageNo, pageSize);
        PageUser pageUser = new PageUser();
        Page<PageUser> ipage = userMapper.selectLikeUserVoPage(page, pageUser, like);
        List<PageUser> list = ipage.getRecords();
        PageUserVo pageUserVo = new PageUserVo().setUsers(list)
                .setCurrent(ipage.getCurrent())
                .setPages(ipage.getPages())
                .setSize(ipage.getSize())
                .setTotal(ipage.getTotal())
                .setHasPrevious(ipage.hasPrevious())
                .setHasNext(ipage.hasNext());


        return pageUserVo;
    }

    /**
     * 更新用户
     *
     * @param pageUser 用户传输对象
     * @return boolean
     */
    @Transactional
    @Override
    public boolean updateUser(PageUser pageUser) {
        RbacUser user = userMapper.selectById(pageUser.getUserId());

        //根据职位名找职位
        Position position = positionService.getOne(new LambdaQueryWrapper<Position>().eq(Position::getPositionName, pageUser.getPositionName()));
        if(position == null){
            //职位为空则新建
            positionService.commit(pageUser.getPositionName(), pageUser.getCompanyName());
            //根据职位名返回职位id
            Position one = positionService.getOne(new LambdaQueryWrapper<Position>().eq(Position::getPositionName, pageUser.getPositionName()));
            user.setPositionId(one.getId());
        }else{
            //职位存在先把职位赋user里
            user.setPositionId(position.getId());
            //再看公司名是否变
            Company company = companyService.getOne(new LambdaQueryWrapper<Company>().eq(Company::getCompanyName, pageUser.getCompanyName()));
            if(company == null){
                //公司为空，添加公司,更新职位中的公司id
                Company one = new Company().setCompanyName(pageUser.getCompanyName());
                companyService.save(one);
                position.setCompanyId(one.getId());
                positionService.updateById(position);
            }else{
                //公司不为空,则直接改职位中公司id
                position.setCompanyId(company.getId());
                positionService.updateById(position);
            }
        }


        //最后改user表剩余
        user.setUserName(pageUser.getUserName());

        userMapper.updateById(user);


        return true;
    }

    @Override
    public List<PermVo> getUserPerm(Integer userId) {
        List<PermVo> list = userMapper.getUserPerm(userId);
        return list;
    }
}
